package com.lsy.weizhi2.popupwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.DiTu;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.myutils.DaShangPopupCall;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 嗨哟 on 2017/3/23.
 */
public class NearbyPopup {
    private PopupWindow nearbyPopup;
    private TextView nearby_title,nearby_content,call_he,interval;
    private ImageView nearby_image;
    private DaShangPopupCall daShangPopupCall;
    public void init(final Context context, View parent, final DiTu.DataBean bean){
        View view=View.inflate(context, R.layout.nearbypopup,null);
        nearby_image= (ImageView) view.findViewById(R.id.nearby_image);
        nearby_title= (TextView) view.findViewById(R.id.nearby_title);
        nearby_content= (TextView) view.findViewById(R.id.nearby_content);
        call_he= (TextView) view.findViewById(R.id.call_he);
        interval= (TextView) view.findViewById(R.id.interval);
        nearby_title.setText(bean.getTitle());
        nearby_content.setText(bean.getContent());
        double distance=DistanceUtil.getDistance(new LatLng(bean.getLatitude(),bean.getLongitude()),new LatLng(MyInfo.Latitude,MyInfo.Longitude));

        String[] str=String.valueOf(distance).split("\\.");
        if(str!=null&&str.length!=0){
            interval.setText("距离:"+str[0]+"米");
        }
        AutoUtils.autoSize(view);
        nearbyPopup=new PopupWindow(context);
        nearbyPopup.setContentView(view);
        nearbyPopup.setWidth(BaseApp.SCREEN_WIDTH*710/750);
        nearbyPopup.setHeight(BaseApp.REAL_HEIGHT*330/1334);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        nearbyPopup.setBackgroundDrawable(dw);
        nearbyPopup.setFocusable(true);
        nearbyPopup.showAtLocation(parent, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,BaseApp.REAL_HEIGHT*120/1334);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson=new Gson();
                String json=gson.toJson(bean);
                daShangPopupCall.yuyue(json);
            }
        });
        call_he.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                daShangPopupCall.connect(view,DaShangPopupCall.CONNECT);
            }
        });
        nearbyPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                daShangPopupCall.dismissListener();
            }
        });
    }

    public boolean isShow(){
        if(nearbyPopup!=null){
            return nearbyPopup.isShowing();
        }else{
            return false;
        }
    }

    public void Dismiss(){
        nearbyPopup.dismiss();
    }

    public void setDaShangPopupCall(DaShangPopupCall daShangPopupCall){
        this.daShangPopupCall=daShangPopupCall;
    }

}
